<?php
namespace app\index\controller;
use think\Exception;

class Account extends Base {
    private $user;

    public function _initialize() {
        parent::_initialize();
        $this->user = $this->getLoginUser();
        if(!$this->user || !$this->user->id){
            $this->redirect(url('users/login'));
        }
        $accountInfo = model('Users')->find(['id' => $this->user->id]);
        $this->assign('user',$accountInfo);
    }

    public function index(){
        return $this->fetch();
    }

    public function edit(){
        //判断是否为post请求
        if(request()->isPost()){
            //处理用户资料修改逻辑
            $data = input('post.'); //获取表单数据
            try{
                model('Users')->updateData($data, $this->user->id);
            }catch (Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('修改成功',url('account/index'));
        }else{
            return $this->fetch();
        }
    }
}